package com.che.libcommon.utils.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.DrawableRes;
import android.text.TextPaint;
import android.widget.TextView;

/**
 * FloatingActionButton 需要设置android:scaleType="center"，否则默认图片显示区域比较小
 * app:fabSize="normal"
 * android:scaleType="center"
 *
 */
 public class FABUtils {


  public static Bitmap createBitmap(Context context, String text, float textSizeSp, int textColor) {
    //
    TextPaint paint = realDeviceTextPaint(context, textSizeSp, textColor);
    float baseline = -paint.ascent();

    int width = (int) (paint.measureText(text) + 0.0f);
    int height = (int) (baseline + paint.descent() + 0.0f);
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);
    canvas.drawText(text, 0, baseline, paint);

    return image;
  }


  public static Bitmap createBitmap(Context context, @DrawableRes int iconRes,int iconWidth,int iconHeight, String text,
      float textSizeSp, int textColor) {
    Bitmap src = resizeBitmap(BitmapFactory.decodeResource(context.getResources(), iconRes),iconWidth,iconHeight);
    int bw = src.getWidth();
    int bh = src.getHeight();

    Paint paint = realDeviceTextPaint(context, textSizeSp, textColor);

    float baseline = - paint.ascent();
    int tw = (int) (paint.measureText(text) + 0.0f);
    int th = (int) (baseline + paint.descent() + 0.0f);

    Bitmap image = Bitmap.createBitmap(Math.max(tw,bw), th+bh, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);

    //draw bitmap
    canvas.save();

    if(tw > bw){
      canvas.translate((tw-bw)/2f,0);
    }
    Rect rect = new Rect(0, 0, bw, bh);
    canvas.drawBitmap(src, rect,new Rect(rect),new Paint(Paint.ANTI_ALIAS_FLAG));
    canvas.restore();


    //draw text

    canvas.save();
    canvas.translate(0,bh);
    if(bw > tw){
      canvas.translate( (bw-tw)/2f,0 );
    }
    paint.setColor(textColor);
    canvas.drawText(text, 0, baseline, paint);
    canvas.restore();

    return image;
  }


  private static Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {
    if (bitmap != null) {
      int width = bitmap.getWidth();
      int height = bitmap.getHeight();
      int newWidth = w;
      int newHeight = h;
      float scaleWight = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;
      Matrix matrix = new Matrix();
      matrix.postScale(scaleWight, scaleHeight);
      return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    } else {
      return null;
    }
  }

  private static TextPaint realDeviceTextPaint(Context context, float textSize, int textColor) {
    TextView textView = new TextView(context);
    textView.setTextSize(textSize);
    textView.setTextColor(textColor);

    return textView.getPaint();
  }
}
